<template>
  <div>
    <vue-upload-web
      ref="upload"
      :multiple="false"
      :form-data="cdnParams"
      :key-generator="keyGenerator"
      url="http://192.168.130.37:8030/fastdfs-services/filenamager/uploadFileText"
      upload-button=".btns"
      @success="handleSuccess"
    />
    <div class="btns">
      <div id="picker" style="height: 20px;line-height: 20px">选择文件</div>
    </div>
    <div>
      <ul>
        <li v-for="(file, index) in files" :key="index" class="is-success">
          {{ file.name }}          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <i class="el-icon-close pointer" @click="handleRemove(index)"></i>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'

const EVENT_REMOVE = 'remove'
const EVENT_SUCCESS = 'success'
const EVENT_CHANGE = 'change'
export default {
  name: 'Upload',
  props: {
    fileList: {
      type: Array,
      default() {
        return []
      }
    },
    isCls: {
      type: Number,
      default: null
    },
    index: {
      type: Number,
      default: null
    },
    length: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      files:[],
      cdnParams: {
        token: '',
        key: '',
        name: '',
        chunk: 0,
        chunks: 1
      }
    }
  },
  mounted() {
    this.files =  _.concat(this.fileList, [])
  },
  methods: {
    handleSuccess(f, res) {
      const file = {
        name: f.name,
        response: {
          url: res.url
        }
      }
      if (this.length === 1) {
        this.files = []
      }
      this.files.push(file)
      this.$emit(EVENT_SUCCESS, file, this.index)
    },
    handleRemove(index) {
      this.files.splice(index, 1)
      this.$emit(EVENT_REMOVE)
    },
    keyGenerator(file) {
      const currentTime = new Date().getTime()
      this.cdnParams.key = 'test/cdn/ie9/' + currentTime + '.' + file.name
      return this.cdnParams.key
    }
  }
}
</script>

<style scoped>
  .btns{
    label{
      height: 25px;
      line-height: 25px;
    }
  }
</style>
